package com.mxl.dao.cashorder.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;

import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.WebApplicationContext;

import com.mxl.common.StringUtil;
import com.mxl.dao.cashorder.model.GatewayPayOrder;
import com.mxl.datebase.orm.hibernate.BaseDaoSupport;
import com.mxl.service.cashorder.dto.CardResultDto;

public class GatewayPayOrderDaoImpl extends
		BaseDaoSupport<GatewayPayOrder, String> {
	private static final Logger logger = LoggerFactory
			.getLogger(GatewayPayOrderDaoImpl.class);

	private static final String CAEDNO_SQL = "SELECT g.ORDERSTATUS,g.SEQID,g.ORDERID,g.CREATIONDATE,g.ERRORCODE,g.MEMBERCODE,g.ORDERAMOUNT,g.ALLOWAMOUNT,p.CARDNO,p.CARDPWD from gatewaypayorder g, purchaseorder p where g.pid = p.id and cardno = ? and g.CREATIONDATE>=DATE_SUB(now(), INTERVAL 31 DAY)";
	private static final String CAEDPWD_SQL = "SELECT g.ORDERSTATUS,g.SEQID,g.ORDERID,g.CREATIONDATE,g.ERRORCODE,g.MEMBERCODE,g.ORDERAMOUNT,g.ALLOWAMOUNT,p.CARDNO,p.CARDPWD from gatewaypayorder g, purchaseorder p where g.pid = p.id and cardpwd = ? and g.CREATIONDATE>=DATE_SUB(now(), INTERVAL 31 DAY)";
	private static final String CAEDNOANDPWD_SQL = "SELECT g.ORDERSTATUS,g.SEQID,g.ORDERID,g.CREATIONDATE,g.ERRORCODE,g.MEMBERCODE,g.ORDERAMOUNT,g.ALLOWAMOUNT,p.CARDNO,p.CARDPWD from gatewaypayorder g, purchaseorder p where g.pid = p.id and cardno = ? and cardpwd = ? and g.CREATIONDATE>=DATE_SUB(now(), INTERVAL 31 DAY)";

	public boolean getMemberCodeOrderId(String membercode, String orderId) {
		List<GatewayPayOrder> gList = null;
		try {
			gList = this.find(Restrictions.eq("memberCode", membercode),
					Restrictions.eq("orderId", orderId));
		} catch (Exception e) {
			logger.error("GatewayPayOrderDaoImpl get memberCode orderId:"
					+ orderId, e);
			return true;
		}
		return gList != null && !gList.isEmpty();
	}

	public List<CardResultDto> query(String cardNo, String cardPwd,
			WebApplicationContext rc) {
		List<CardResultDto> gList = new ArrayList<CardResultDto>();

		if (!StringUtil.isEmpty(cardNo) && !StringUtil.isEmpty(cardPwd)) {
			trsOrder(new Object[] { cardNo, cardPwd }, rc, gList,
					CAEDNOANDPWD_SQL);
		} else if (!StringUtil.isEmpty(cardNo)) {
			trsOrder(new Object[] { cardNo }, rc, gList, CAEDNO_SQL);
		} else if (!StringUtil.isEmpty(cardPwd)) {
			trsOrder(new Object[] { cardPwd }, rc, gList, CAEDPWD_SQL);
		}
		return gList;
	}

	private void trsOrder(Object[] obj, WebApplicationContext rc,
			List<CardResultDto> gList, String sql) {
		List<Map> lMaps = this.executeSqlMap(sql, obj);
		CardResultDto dto = null;
		Map m = null;
		for (int i = 0; i < lMaps.size(); i++) {
			dto = new CardResultDto();
			m = lMaps.get(i);
			dto.setCardAmount(m.get("ALLOWAMOUNT") != null ? String
					.valueOf(Long.valueOf(m.get("ALLOWAMOUNT").toString()) / 100)
					: "0");
			dto.setOrderAmount(String.valueOf(Long.valueOf(m.get("ORDERAMOUNT")
					.toString()) / 100));
			dto.setCardNo((String) m.get("CARDNO"));
			dto.setCardPwd((String) m.get("CARDPWD"));
			dto.setDate(m.get("CREATIONDATE").toString());
			dto.setErrorDesc(rc.getMessage((String) m.get("ERRORCODE"), null,
					Locale.CHINESE));
			dto.setOrderId((String) m.get("ORDERID"));
			dto.setSeqId((String) m.get("SEQID"));
			dto.setStatus(getStatus(m.get("ORDERSTATUS").toString()));
			gList.add(dto);
		}
	}

	private String getStatus(String status) {
		String result = "处理进行中";
		if ("111".equals(status)) {
			result = "支付成功";
		} else if ("112".equals(status)) {
			result = "支付失败";
		}
		return result;
	}

	public GatewayPayOrder getPaymentOrder(String seqNo) {
		return this.findUnique(Restrictions.eq("seqNo", seqNo));
	}

}
